package com.lh0811.forge.pserv.uacs.server.component.data_scope.provider;

import com.lh0811.forge.dependency.security.client.component.SecurityUtil;
import com.lh0811.forge.dependency.security.core.model.entity.SecurityAuthority;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;

/**
 * 规则值提供
 */
@Slf4j
public abstract class AbstractSysRuleValueProvider<T> implements SysRuleValueProvider<T> {

    public T adapterPageHelper() throws Exception {
        // 创建一个线程池
        ExecutorService executor = Executors.newSingleThreadExecutor();

        SecurityAuthority securityAuthority = null;
        try {
            securityAuthority = SecurityUtil.getSecurityAuthority();
        } catch (Exception e) {
            log.info("无用户信息");
        }

        // 将 securityAuthority 声明为 final
        final SecurityAuthority finalSecurityAuthority = securityAuthority;

        // 创建 FutureTask 来包装 Callable 任务
        FutureTask<T> futureTask = new FutureTask<>(new Callable<T>() {
            @Override
            public T call() throws Exception {
                return getRuleValueByCurrentUser(finalSecurityAuthority);
            }
        });

        // 提交任务到线程池
        executor.execute(futureTask);

        // 在主线程中等待子线程的结果
        T ruleValueByCurrentUser = futureTask.get();

        // 关闭线程池
        executor.shutdown();

        return ruleValueByCurrentUser;
    }


}
